package com.atguigu.guli.service.base.handler;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author lsystart
 * @create2021-10-26 19:49
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    //最大异常的处理器
    @ExceptionHandler(value = Exception.class)
    public R exception(Exception e){
        log.error("{}" , ExceptionUtils.getStackTrace(e));
        return R.error();
    }
    //为要处理的异常编写异常处理器
    @ExceptionHandler(value = ArithmeticException.class)
    public R exception(ArithmeticException e){//形参必须和异常处理器注解处理异常类型一样
        log.error("{}" , ExceptionUtils.getStackTrace(e));
        return R.setResult(ResultCodeEnum.DIV_ZERO_ERROR);
    }
}
